/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.femass.repository.infra;

import com.femass.model.Revisor;
import com.femass.repository.RevisorRepository;
import com.femass.util.FacesUtil;
import java.util.List;
import javax.faces.application.FacesMessage;
import org.hibernate.HibernateException;
import org.hibernate.Session;
import org.hibernate.criterion.Restrictions;

/**
 *
 * @author daureo
 */
public class RevisorRepositoryHibernate implements RevisorRepository{

    @Override
    public List<Revisor> todos() {
        Session session = (Session) FacesUtil.getRequestAttribute("session");
        return session.createCriteria(Revisor.class)
                .list();
    }

    @Override
    public Revisor porCodigo(Integer codigo) {
         Session session = (Session) FacesUtil.getRequestAttribute("session");
         return (Revisor) session.get(Revisor.class, new Integer(codigo));
    }

    @Override
    public void salvar(Revisor revisor) {
        Session session = (Session) FacesUtil.getRequestAttribute("session");
       try{
            session.merge(revisor);                       
            FacesUtil.adicionarMensagem(FacesMessage.SEVERITY_INFO, "Revisor Salvo!");
       } catch(Exception e){
            FacesUtil.adicionarMensagem(FacesMessage.SEVERITY_WARN, "Erro ao salvar Revisor!");
       }
    }

    @Override
    public void remover(Revisor revisor) {
        Session session = (Session) FacesUtil.getRequestAttribute("session");
      
        try {        
            session.delete(revisor);
            FacesUtil.adicionarMensagem(FacesMessage.SEVERITY_INFO, "Revisor excluído com sucesso!");
        } catch (HibernateException e) {
            FacesUtil.adicionarMensagem(FacesMessage.SEVERITY_INFO, "Revisor não excluído!");
        } 
    }

    @Override
    public Revisor comDadosIguais(Revisor revisor) {
       Session session = (Session) FacesUtil.getRequestAttribute("session");
        return (Revisor) session.createCriteria(Revisor.class)
                .add(Restrictions.eq("nome", revisor.getNome()))
                .uniqueResult();
    }
    
}
